﻿using System;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using com.google.zxing;
using com.google.zxing.common;
using QRCodeReader.Common;

namespace QRCodeReader
{
    public partial class MainWindow
    {
        public double StartPointX;
        public double StartPointY;
        public double width;
        public bool IsMouseDown;

        private com.google.zxing.qrcode.QRCodeReader reader;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void WindowMouseDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown = true;
            StartPointX = e.GetPosition(null).X;
            StartPointY = e.GetPosition(null).Y;
        }

        private void WindowMouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                var posX = e.GetPosition(null).X;
                var rectangle = new System.Windows.Shapes.Rectangle();
                var brush = new SolidColorBrush(Colors.White);
                rectangle.Stroke = brush;
                rectangle.Fill = brush;
                rectangle.StrokeThickness = 1;
                rectangle.Width = rectangle.Height = Math.Abs(posX - StartPointX);
                cnv.Children.Clear();
                cnv.Children.Add(rectangle);
                Canvas.SetLeft(rectangle, StartPointX);
                Canvas.SetTop(rectangle, StartPointY);
                if (e.LeftButton == MouseButtonState.Released)
                {
                    cnv.Children.Clear();
                    width = e.GetPosition(null).X - StartPointX;
                    CaptureScreen(StartPointX, StartPointY, width);
                    StartPointX = StartPointY = 0;
                    IsMouseDown = false;
                }
            }
        }

        private void CaptureScreen(double pointX, double pointY, double squareWidth)
        {
            var bitmap = ScreenCapture.CaptureRegion((int)pointX - 5, 
                                                     (int)pointY - 5, 
                                                     (int)squareWidth, 
                                                     (int)squareWidth).ToBitmap();

            reader = new com.google.zxing.qrcode.QRCodeReader();

            var source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);

            try
            {
                var result = reader.decode(binBitmap);
                Dispatcher.BeginInvoke(new Action(() => ShowResult(result.Text)));
            }
            catch (Exception)
            {

            } 

            ////Saving File in Desktop
            //var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
            //            DateTime.Now.ToFileTime().ToString() + ".png");
            //bitmap.Save(path, ImageFormat.Png);
        }

        private void ShowResult(string text)
        {
            MessageBox.Show(text, "QR Code Info");
        }

        private void WindowKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                Close();
            }
        }
    }
}